﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dos.Common;
using Dos.ORM;

namespace Business
{
    /// <summary>
    /// 
    /// </summary>
    public class Db
    {
        public static readonly DbSession Context = new DbSession("MySqlConn");
        //public static readonly DbSession erpdb = new DbSession("MySqlerpdb");

        //public static readonly DbSession Context = new DbSession("SqlServerConn");
        //public static readonly DbSession Context = new DbSession("SqliteConn");
        //public static readonly DbSession Context = new DbSession("AccessConn");
        //public static readonly DbSession Context = new DbSession("OracleConn");
        //public static readonly DbSession Context = new DbSession("PostgreSqlConn");
        static Db()
        {
            Context.RegisterSqlLogger(delegate (string sql)
            {
                //在此可以记录sql日志
                //写日志会影响性能，建议开发版本记录sql以便调试，发布正式版本不要记录
                LogHelper.Debug(sql, "SQL日志");
            });
        }

        public static DbSession GetDb(string city = "")
        {
            if (city == "gl")
            {
                return Db.Context;
            }
            return Db.Context;
        }


        public static T GetJObject<T>(string json)
        {
            var result = JSON.ToObject<T>(json);
            return (T)result;
        }

        public static EntityState GetEs<T>(T t)
        {
            int id = Convert.ToInt32(t.GetType().GetProperty("id").GetValue(t, null));
            if (id <= 0)
            {
                return EntityState.Added;
            }
            return EntityState.Added;
        }
        public static EntityState GetEs(int id)
        {
            if (id <= 0)
            {
                return EntityState.Added;
            }
            return EntityState.Modified;
        }


        //public static T Save<T>(T t, string city = "")
        //{
        //    int id = Convert.ToInt32(t.GetType().GetProperty("id").GetValue(t, null));
        //    if (id == 0)
        //    {
        //        //id = Convert.ToInt32(Context.Insert(t));
        //        //t.GetType().GetProperty("id").SetValue(t, id);
        //    }
        //    //Context.Save()

        //    //else {
        //    //    Context.Update(t);
        //    //}
        //    return t;
        //}

        /// <summary>
        /// 获取传入对象的属性值，格式为(属性：值),
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns>属性与值拼接成的字符串，如(属性：值),(属性：值),(属性：值),</returns>
        public static int GetProperties<T>(T t, string sname)
        {
            int tStr = 0;
            if (t == null)
            {
                return tStr;
            }

            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return tStr;
            }

            foreach (System.Reflection.PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(t, null);

                if (sname.ToLower() == name.ToLower()) {
                    return Convert.ToInt32(value);
                }
            }
            return tStr;
        }


    }


}



///// <summary>
///// 获取传入对象的属性值，格式为(属性：值),
///// </summary>
///// <typeparam name="T"></typeparam>
///// <param name="t"></param>
///// <returns>属性与值拼接成的字符串，如(属性：值),(属性：值),(属性：值),</returns>
//public static string GetProperties<T>(T t)
//{
//    string tStr = string.Empty;
//    if (t == null)
//    {
//        return tStr;
//    }
//    System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
//    if (properties.Length <= 0)
//    {
//        return tStr;
//    }
//    foreach (System.Reflection.PropertyInfo item in properties)
//    {
//        string name = item.Name;
//        object value = item.GetValue(t, null);
//        if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
//        {
//            tStr += string.Format("{0}:{1},", name, value);
//        }
//        else
//        {
//            GetProperties(value);
//        }
//    }
//    return tStr;
//}